package cn.sibu.disalllowtouchintercept;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.IOException;
import java.util.UUID;

public class SearchActivity extends AppCompatActivity  implements RestTask.ProgressCallback,RestTask.ResponseCallback{

    private TextView mResult;
    private ProgressDialog mProgress;
    UUID uuid = UUID.randomUUID();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ScrollView scrollView = new ScrollView(this);
        mResult = new TextView(this);
        scrollView.addView(mResult,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setContentView(scrollView);

        //创建请求

        //简单的get请求
        String url = "http://mw.vmall.com/homeAdInfo.json";
        try {
            RestTask getTask = RestUtil.obtainGetTask(url);
            getTask.setProgressCallback(this);
            getTask.setResponseCallback(this);
            getTask.execute();
            //向用户显示进度
            mProgress = ProgressDialog.show(this,"Searching","Waiting for result",true);

        } catch (IOException e) {
            mResult.setText(e.getMessage());
        }

    }


    @Override
    public void onProgressUpdate(int progress) {
        if(progress>=0){
            if(mProgress!=null){
                mProgress.dismiss();
                mProgress=null;
            }
            //更新用户的进度
            mResult.setText(String.format("Download Progress: %d%%",progress));
        }
    }

    @Override
    public void onRequestSucces(String response) {
        //结束进度条
        if(mProgress != null){
            mProgress.dismiss();
        }
        //处理返回的数据
        mResult.setText(response);
    }

    @Override
    public void onRequestError(Exception error) {
            //结束进度条
        if(mProgress != null){
            mProgress.dismiss();
        }
        //处理返回的数据(这里只是把结果显示出来)
        mResult.setText("An Error Occurred :" + error.getMessage());

    }
}
